package com.isman.placeslist;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.isman.placeslist.apiobject.Session;
import com.isman.placeslist.setting.ProjectPref;
import com.isman.placeslist.ws.AsyncRunnerHeadler;
import com.isman.placeslist.ws.ServiceProvider;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class PageSingin extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_singin);
		vBtnSingin = (Button) findViewById(R.id.btn_singin_singin);
		vEtEmail = (EditText) findViewById(R.id.et_singin_email);
		vEtPass = (EditText) findViewById(R.id.et_singin_password);
		vBtnSingin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String email = vEtEmail.getText().toString();
				String pass = vEtPass.getText().toString();
				mSingIn(email, pass);
				
			}
		});
		ProjectPref.clear(this);

	}

	protected void mSingIn(String email, String pass) {
		mDialog = ProgressDialog.show(PageSingin.this, "", "Singing In");
		ServiceProvider provider = new ServiceProvider(PageSingin.this);
		provider.SingIn(email, pass, new AsyncRunnerHeadler() {
			@Override
			public void onError(final int statusCode, final String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mDialog.cancel();
						if (statusCode == 402) {
							Toast.makeText(PageSingin.this,
									"email or password incorrect.",
									Toast.LENGTH_LONG).show();
						}
					}
				});
			}

			@Override
			public void onConnectingError(final String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mDialog.cancel();
						Toast.makeText(PageSingin.this, "ConnectError " + json,
								Toast.LENGTH_LONG).show();
					}
				});
			}

			@Override
			public void onComplete(final String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mDialog.cancel();
						Intent intent = new Intent();
						try {
							Session singin = new Gson().fromJson(json,
									Session.class);
							intent.putExtra(PageMain.SINGIN_INFO, singin);
						} catch (JsonSyntaxException e) {
							e.printStackTrace();
						}
						setResult(RESULT_OK, intent);
						finish();
					}
				});

			}

			@Override
			public void onAuthenticate(final String json) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mDialog.cancel();
						Toast.makeText(PageSingin.this, json, Toast.LENGTH_LONG)
								.show();

					}
				});

			}
		});
	}

	private ProgressDialog mDialog;

	private EditText vEtPass;
	private EditText vEtEmail;
	private Button vBtnSingin;
}
